<template>
  <el-form :model="newBanner" label-width="auto" style="max-width: 600px">
    <el-form-item label="图片地址">
      <el-input v-model="newBanner.img" />
    </el-form-item>
    <el-form-item label="图片连接">
      <el-input v-model="newBanner.link" />
    </el-form-item>
    <el-form-item label="图片提示">
      <el-input v-model="newBanner.alt" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="addBanner">添加</el-button>
      <el-button>Cancel</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive } from "vue";
import type { IBanner } from "@/myTypes";
import { addBannerApi } from "@/api/banners";
import { ElMessage } from "element-plus";

const banner: IBanner = {
  img: "",
  link: "",
  alt: "",
};
let newBanner = reactive<IBanner>(banner);

const addBanner = async () => {
  // console.log('submit!',form)
  const res = await addBannerApi(newBanner);

  if (res.data.code === "200") {
    ElMessage.success("添加成功！");
  }

  clearBanner();
};

function clearBanner() {
  // newBanner = reactive({...banner});

  let key: keyof IBanner;
  for (key in newBanner) {
    // console.log(`${key}: ${newBanner[key]}`);
    newBanner[key] = (key!=="flag"?"":false);
    // newBanner[key] = "";
  }
}
</script>
